<?
/***************************************************************************
*                            Dolphin Smart Community Builder
*                              -------------------
*     begin                : Mon Mar 23 2006
*     copyright            : (C) 2007 BoonEx Location
*     website              : http://www.boonex.com
* This file is part of Dolphin - Smart Community Builder
*
* Dolphin is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the
* License, or  any later version.
*
* Dolphin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Dolphin,
* see license.txt file; if not, write to marketing@boonex.com
***************************************************************************/

bx_import('BxDolTwigTemplate');
bx_import('BxDolCategories');

/*
 * Location module View
 */
class BxLocationTemplate extends BxDolTwigTemplate {

    var $_iPageIndex = 500;      
    var $oDb;

	/**
	 * Constructor
	 */
	function BxLocationTemplate(&$oConfig, &$oDb) {
        parent::BxDolTwigTemplate($oConfig, $oDb);

		$this->oDb = $oDb; 

    }
  
    function blockSubProfileInfo ($sType, &$aData) {

		$this->_oMain = BxDolModule::getInstance('BxLocationModule');

        $aAuthor = getProfileInfo($aData['author_id']);
 
        $aVars = array (
            'author_thumb' => get_member_thumbnail($aAuthor['ID'], 'none'),
            'date' => getLocaleDate($aData['created'], BX_DOL_LOCALE_DATE_SHORT),
            'date_ago' => defineTimeInterval($aData['created']),
            'cats' => $this->parseSubCategories($sType, $aData['categories']),
            'tags' => $this->parseSubTags($aData['tags']),
            'country_city' => '',//_t($GLOBALS['aPreValues']['Country'][$aData['country']]['LKey']) . (trim($aData['city']) ? ', '.$aData['city'] : ''),
            'flag_image' => '',//genFlag($aData['country']),
            'fields' => $this->blockSubProfileFields($sType, $aData),
            'author_username' => $aAuthor ? $aAuthor['NickName'] : _t('_modzzz_location_admin'),
            'author_url' => $aAuthor ? getProfileLink($aAuthor['ID']) : 'javascript:void(0)',
        );
        return $this->parseHtmlByName('block_subprofile_info', $aVars);
    }

	function review_unit($aReview){
        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxLocationModule');
  
		$iEntryId = $aReview['id']; 
		$iLocationId = $aReview['location_id']; 
		$iSubId = $aReview['sub_id']; 

		$sType = $aReview['type'];
		if($sType=='location'){
			$aLocation = $this->_oDb->getEntryById($iLocationId);
			
			$sTitle = $aLocation['title'];
			$sUrl = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'view/' . $aLocation['uri'];
		}else{
			$aSubProfile = $this->_oDb->getSubProfileEntryById($sType, $iSubId);
			$aLocation = $this->_oDb->getEntryById($iLocationId);
			
			$sTitle = $aSubProfile['title'];
			$sUrl = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . $sType .'/view/' . $aSubProfile['uri'];
		}

		$iReviewId = (int)$aReview['id'];
		$iMemberId = $aReview['member_id'];
		$sNickName = getNickName($iMemberId);
		$sNickLink = getProfileLink($iMemberId);
		$icoMember = get_member_thumbnail($iMemberId, 'left');
		$sMessage = $aReview['desc']; 
		$dtSent = date("M jS, Y g:i A", $aReview['created']);
	 
		$bAdmin = $this->_oMain->isAdmin() || $this->_oMain->isEntryAdmin($aLocation) || ($iMemberId==$this->_oMain->_iProfileId);

        $aVars = array (            
			'entry_id' => $iEntryId, 
			'review_id' => $iReviewId, 
			'review_for' => $sTitle, 
			'review_for_url' => $sUrl, 
			'ico_member' => $icoMember, 
			'nicklink' => $sNickLink,
			'nickname' => $sNickName,
			'date' => $dtSent,
			'message' => $sMessage,
			'bx_if:allowdelete' => array( 
				'condition' =>  $bAdmin,
				'content' => array(
					'entry_id' => $iEntryId, 
					'review_id' => $iReviewId, 
					'url' => $sUrl,   
				), 
			),
        ); 
 
        return $this->parseHtmlByName('review_unit', $aVars);
    }
  
    function event_unit ($aData, $sTemplateName, &$oVotingView) {
 
        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxLocationModule');
 
		$aEventEntry = $this->_oDb->getEventEntryById($aData['id']);
		$iEntryId = $aEventEntry['location_id'];
 
        $aDataEntry = $this->_oDb->getEntryById($iEntryId);

        if (!$this->_oMain->isAllowedView ($aDataEntry)) {            
            $aVars = array ('extra_css_class' => 'modzzz_location_unit');
            return $this->parseHtmlByName('browse_unit_private', $aVars);
        }
 

        $sImage = '';
        if ($aData['thumb']) {
            $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
            $aImage = BxDolService::call('photos', 'get_image', array($a, 'browse'), 'Search');
            $sImage = $aImage['no_image'] ? '' : $aImage['file'];
        } 

        $aVars = array (            
            'id' => $aData['id'],
            'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'),
            'event_url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'event/view/' . $aData['uri'],
            'event_title' => $aData['title'],
            'event_start' => defineTimeInterval($aData['event_start']),
            'author' => $aData['author_id'] ? getNickName($aData['author_id']) : _t('_modzzz_location_admin'),
            'author_url' => $aData['author_id'] ? getProfileLink($aData['author_id']) : 'javascript:void(0);',
            'author_title' => _t('_From'),
            'spacer' => getTemplateIcon('spacer.gif'),
            'participants' => $aData['fans_count'],
            'address' => $aData['address1'], 
            'country_city' => _t($GLOBALS['aPreValues']['Country'][$aDataEntry['country']]['LKey']) . (trim($aDataEntry['city']) ? ', '.$aDataEntry['city'] : ''),
        ); 
 
        $aVars['rate'] = $oVotingView ? $oVotingView->getJustVotingElement(0, $aData['id'], $aData['rate']) : '&#160;';

        //$aVars = array_merge ($aVars, $aData);
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }
 
    function food_unit ($aData, $sTemplateName, &$oVotingView) {
 
        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxLocationModule');
 
		$aEntry = $this->_oDb->getFoodEntryById($aData['id']);
		$iEntryId = $aEntry['location_id'];
 
        $aDataEntry = $this->_oDb->getEntryById($iEntryId);

        if (!$this->_oMain->isAllowedView ($aDataEntry)) {            
            $aVars = array ('extra_css_class' => 'modzzz_location_unit');
            return $this->parseHtmlByName('browse_unit_private', $aVars);
        }
 

        $sImage = '';
        if ($aData['thumb']) {
            $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
            $aImage = BxDolService::call('photos', 'get_image', array($a, 'browse'), 'Search');
            $sImage = $aImage['no_image'] ? '' : $aImage['file'];
        } 

        $aVars = array (            
            'id' => $aData['id'],
            'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'),
            'url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'food/view/' . $aData['uri'],
            'title' => $aData['title'],
            'author' => $aData['author_id'] ? getNickName($aData['author_id']) : _t('_modzzz_location_admin'),
            'author_url' => $aData['author_id'] ? getProfileLink($aData['author_id']) : 'javascript:void(0);',
            'author_title' => _t('_From'),
            'spacer' => getTemplateIcon('spacer.gif'),
            'address' => $aData['address1'], 
            'country_city' => _t($GLOBALS['aPreValues']['Country'][$aDataEntry['country']]['LKey']) . (trim($aDataEntry['city']) ? ', '.$aDataEntry['city'] : ''),
        ); 
 
        $aVars['rate'] = $oVotingView ? $oVotingView->getJustVotingElement(0, $aData['id'], $aData['rate']) : '&#160;';

        //$aVars = array_merge ($aVars, $aData);
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }
 
    function nightlife_unit ($aData, $sTemplateName, &$oVotingView) {
 
        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxLocationModule');
 
		$aEntry = $this->_oDb->getNightlifeEntryById($aData['id']);
		$iEntryId = $aEntry['location_id'];
 
        $aDataEntry = $this->_oDb->getEntryById($iEntryId);

        if (!$this->_oMain->isAllowedView ($aDataEntry)) {            
            $aVars = array ('extra_css_class' => 'modzzz_location_unit');
            return $this->parseHtmlByName('browse_unit_private', $aVars);
        }
  
        $sImage = '';
        if ($aData['thumb']) {
            $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
            $aImage = BxDolService::call('photos', 'get_image', array($a, 'browse'), 'Search');
            $sImage = $aImage['no_image'] ? '' : $aImage['file'];
        } 
 
        $aVars = array (            
            'id' => $aData['id'],
            'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'),
            'url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'nightlife/view/' . $aData['uri'],
            'title' => $aData['title'],
            'author' => $aData['author_id'] ? getNickName($aData['author_id']) : _t('_modzzz_location_admin'),
            'author_url' => $aData['author_id'] ? getProfileLink($aData['author_id']) : 'javascript:void(0);',
            'author_title' => _t('_From'),
            'spacer' => getTemplateIcon('spacer.gif'),
            'address' => $aData['address1'],
            'country_city' => _t($GLOBALS['aPreValues']['Country'][$aDataEntry['country']]['LKey']) . (trim($aDataEntry['city']) ? ', '.$aDataEntry['city'] : ''),
        ); 
 
        $aVars['rate'] = $oVotingView ? $oVotingView->getJustVotingElement(0, $aData['id'], $aData['rate']) : '&#160;';

        //$aVars = array_merge ($aVars, $aData);
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }
 
    function transport_unit ($aData, $sTemplateName, &$oVotingView) {
 
        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxLocationModule');
 
		$aEntry = $this->_oDb->getTransportEntryById($aData['id']);
		$iEntryId = $aEntry['location_id'];
 
        $aDataEntry = $this->_oDb->getEntryById($iEntryId);

        if (!$this->_oMain->isAllowedView ($aDataEntry)) {            
            $aVars = array ('extra_css_class' => 'modzzz_location_unit');
            return $this->parseHtmlByName('browse_unit_private', $aVars);
        }
 
        $sImage = '';
        if ($aData['thumb']) {
            $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
            $aImage = BxDolService::call('photos', 'get_image', array($a, 'browse'), 'Search');
            $sImage = $aImage['no_image'] ? '' : $aImage['file'];
        } 

        $aVars = array (            
            'id' => $aData['id'],
            'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'),
            'url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'transport/view/' . $aData['uri'],
            'title' => $aData['title'],
            'author' => $aData['author_id'] ? getNickName($aData['author_id']) : _t('_modzzz_location_admin'),
            'author_url' => $aData['author_id'] ? getProfileLink($aData['author_id']) : 'javascript:void(0);',
            'author_title' => _t('_From'),
            'spacer' => getTemplateIcon('spacer.gif'),
            'address' => $aData['address1'],
            'country_city' => _t($GLOBALS['aPreValues']['Country'][$aDataEntry['country']]['LKey']) . (trim($aDataEntry['city']) ? ', '.$aDataEntry['city'] : ''),
        ); 
 
        $aVars['rate'] = $oVotingView ? $oVotingView->getJustVotingElement(0, $aData['id'], $aData['rate']) : '&#160;';

        //$aVars = array_merge ($aVars, $aData);
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }
  
    function shop_unit ($aData, $sTemplateName, &$oVotingView) {
 
        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxLocationModule');
 
		$aEntry = $this->_oDb->getShopEntryById($aData['id']);
		$iEntryId = $aEntry['location_id'];
 
        $aDataEntry = $this->_oDb->getEntryById($iEntryId);

        if (!$this->_oMain->isAllowedView ($aDataEntry)) {            
            $aVars = array ('extra_css_class' => 'modzzz_location_unit');
            return $this->parseHtmlByName('browse_unit_private', $aVars);
        }
  
        $sImage = '';
        if ($aData['thumb']) {
            $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
            $aImage = BxDolService::call('photos', 'get_image', array($a, 'browse'), 'Search');
            $sImage = $aImage['no_image'] ? '' : $aImage['file'];
        } 

        $aVars = array (            
            'id' => $aData['id'],
            'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'),
            'url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'shop/view/' . $aData['uri'],
            'title' => $aData['title'],
            'author' => $aData['author_id'] ? getNickName($aData['author_id']) : _t('_modzzz_location_admin'),
            'author_url' => $aData['author_id'] ? getProfileLink($aData['author_id']) : 'javascript:void(0);',
            'author_title' => _t('_From'),
            'spacer' => getTemplateIcon('spacer.gif'),
            'address' => $aData['address1'],
            'country_city' => _t($GLOBALS['aPreValues']['Country'][$aDataEntry['country']]['LKey']) . (trim($aDataEntry['city']) ? ', '.$aDataEntry['city'] : ''),
        ); 
 
        $aVars['rate'] = $oVotingView ? $oVotingView->getJustVotingElement(0, $aData['id'], $aData['rate']) : '&#160;';

        //$aVars = array_merge ($aVars, $aData);
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }
 

    function service_unit ($aData, $sTemplateName, &$oVotingView) {
 
        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxLocationModule');
 
		$aEntry = $this->_oDb->getServiceEntryById($aData['id']);
		$iEntryId = $aEntry['location_id'];
 
        $aDataEntry = $this->_oDb->getEntryById($iEntryId);

        if (!$this->_oMain->isAllowedView ($aDataEntry)) {            
            $aVars = array ('extra_css_class' => 'modzzz_location_unit');
            return $this->parseHtmlByName('browse_unit_private', $aVars);
        }
  
        $sImage = '';
        if ($aData['thumb']) {
            $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
            $aImage = BxDolService::call('photos', 'get_image', array($a, 'browse'), 'Search');
            $sImage = $aImage['no_image'] ? '' : $aImage['file'];
        } 

        $aVars = array (            
            'id' => $aData['id'],
            'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'),
            'url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'service/view/' . $aData['uri'],
            'title' => $aData['title'],
            'author' => $aData['author_id'] ? getNickName($aData['author_id']) : _t('_modzzz_location_admin'),
            'author_url' => $aData['author_id'] ? getProfileLink($aData['author_id']) : 'javascript:void(0);',
            'author_title' => _t('_From'),
            'spacer' => getTemplateIcon('spacer.gif'),
            'address' => $aData['address1'],
            'country_city' => _t($GLOBALS['aPreValues']['Country'][$aDataEntry['country']]['LKey']) . (trim($aDataEntry['city']) ? ', '.$aDataEntry['city'] : ''),
        ); 
 
        $aVars['rate'] = $oVotingView ? $oVotingView->getJustVotingElement(0, $aData['id'], $aData['rate']) : '&#160;';

        //$aVars = array_merge ($aVars, $aData);
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }
 
    function school_unit ($aData, $sTemplateName, &$oVotingView) {
 
        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxLocationModule');
 
		$aEntry = $this->_oDb->getSchoolEntryById($aData['id']);
		$iEntryId = $aEntry['location_id'];
 
        $aDataEntry = $this->_oDb->getEntryById($iEntryId);

        if (!$this->_oMain->isAllowedView ($aDataEntry)) {            
            $aVars = array ('extra_css_class' => 'modzzz_location_unit');
            return $this->parseHtmlByName('browse_unit_private', $aVars);
        }
  
        $sImage = '';
        if ($aData['thumb']) {
            $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
            $aImage = BxDolService::call('photos', 'get_image', array($a, 'browse'), 'Search');
            $sImage = $aImage['no_image'] ? '' : $aImage['file'];
        } 

        $aVars = array (            
            'id' => $aData['id'],
            'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'),
            'url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'school/view/' . $aData['uri'],
            'title' => $aData['title'],
            'author' => $aData['author_id'] ? getNickName($aData['author_id']) : _t('_modzzz_location_admin'),
            'author_url' => $aData['author_id'] ? getProfileLink($aData['author_id']) : 'javascript:void(0);',
            'author_title' => _t('_From'),
            'spacer' => getTemplateIcon('spacer.gif'),
            'address' => $aData['address1'],
            'country_city' => _t($GLOBALS['aPreValues']['Country'][$aDataEntry['country']]['LKey']) . (trim($aDataEntry['city']) ? ', '.$aDataEntry['city'] : ''),
        ); 
 
        $aVars['rate'] = $oVotingView ? $oVotingView->getJustVotingElement(0, $aData['id'], $aData['rate']) : '&#160;';

        //$aVars = array_merge ($aVars, $aData);
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }
 

    function hotel_unit ($aData, $sTemplateName, &$oVotingView) {
 
        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxLocationModule');
 
		$aEntry = $this->_oDb->getHotelEntryById($aData['id']);
		$iEntryId = $aEntry['location_id'];
 
        $aDataEntry = $this->_oDb->getEntryById($iEntryId);

        if (!$this->_oMain->isAllowedView ($aDataEntry)) {            
            $aVars = array ('extra_css_class' => 'modzzz_location_unit');
            return $this->parseHtmlByName('browse_unit_private', $aVars);
        }
 

        $sImage = '';
        if ($aData['thumb']) {
            $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
            $aImage = BxDolService::call('photos', 'get_image', array($a, 'browse'), 'Search');
            $sImage = $aImage['no_image'] ? '' : $aImage['file'];
        } 

        $aVars = array (            
            'id' => $aData['id'],
            'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'),
            'url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'hotel/view/' . $aData['uri'],
            'title' => $aData['title'],
             'author' => $aData['author_id'] ? getNickName($aData['author_id']) : _t('_modzzz_location_admin'),
            'author_url' => $aData['author_id'] ? getProfileLink($aData['author_id']) : 'javascript:void(0);',
            'author_title' => _t('_From'),
            'spacer' => getTemplateIcon('spacer.gif'),
            'address' => $aData['address1'],
            'country_city' => _t($GLOBALS['aPreValues']['Country'][$aDataEntry['country']]['LKey']) . (trim($aDataEntry['city']) ? ', '.$aDataEntry['city'] : ''),
        ); 
 
        $aVars['rate'] = $oVotingView ? $oVotingView->getJustVotingElement(0, $aData['id'], $aData['rate']) : '&#160;';

        //$aVars = array_merge ($aVars, $aData);
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }
 

     function claim_unit ($aData, $sTemplateName, &$oVotingView) {

        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxLocationModule');
 
 		$aCategory = $this->oDb->getCategoryInfo($aData['category_id']);
		$sCategoryUrl = $this->oDb->getSubCategoryUrl($aCategory['uri']);
 
        $aAuthor = getProfileInfo($aData['author_id']);
  
        $sImage = '';
        if ($aData['thumb']) {
            $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
            $aImage = BxDolService::call('photos', 'get_image', array($a, 'browse'), 'Search');
            $sImage = $aImage['no_image'] ? '' : $aImage['file'];
        } 
 
        modzzz_location_import('Voting');
        $oRating = new BxLocationVoting ('modzzz_location', $aData['id']);
 
		$sOwnerThumb = $GLOBALS['oFunctions']->getMemberIcon($aAuthor['ID'], 'left'); 
	  
		$sDateTime = defineTimeInterval($aData['created']);
     
		$sPostText = $aData['desc'];
		$iLimitChars = (int)getParam('modzzz_location_max_preview');
		if (strlen($sPostText) > $iLimitChars) {
 			$sLinkMore = '...';
			$sPostText = mb_substr(strip_tags($sPostText), 0, $iLimitChars) . $sLinkMore;
		}
 
        $aVars = array (
 		    'id' => $aData['id'],  
			'image_url' => !$aImage['no_image'] && $aImage['file'] ? $aImage['file'] : $this->getIconUrl('no-photo-110.png'),
            'image_title' => !$aImage['no_image'] && $aImage['title'] ? $aImage['title'] : '',        
			
            'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'), 
    
            'location_url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'view/' . $aData['uri'],
            'location_title' => $aData['title'],
            'location_description' => $sPostText,
            'comments_count' => $aData['comments_count'],
            'post_date' => strtolower($sDateTime), 
            'post_uthumb' => $sOwnerThumb,
            'post_picture2' => $sPostPicture, 
            'all_categories' => '<a href="'.$sCategoryUrl.'">'.$aCategory['name'].'</a>',  
            'post_tags' => $this->_oMain->parseTags($aData['tags']), 
            'author_title' => _t('_From'),
            'author_username' => $sAuthorName,
            'author_url' => $sAuthorLink,
            'rating' => $oRating->isEnabled() ? $oRating->getJustVotingElement (true, $aData['id']) : '',
			'claim_message' => $aData['message'],
			'claimant_url' => getProfileLink($aData['member_id']),
			'claimant_name' => getNickName($aData['member_id']),
			'claim_date' => $this->filterDate($aData['claim_date'], true), 

        );
 
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }

    function order_unit ($aData, $sTemplateName, &$oVotingView) {

        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxLocationModule');
  
        $sAuthorName = getNickName($aData['author_id']); 
		$sAuthorLink = getProfileLink($aData['author_id']);  
 		$sCreateDate = $this->filterDate($aData['order_date']);
  		$sDueDate = $this->filterDate($aData['expiry_date']);
		$sPackageName = $this->_oMain->_oDb->getPackageName($aData['package_id']);
      
        $aVars = array (
 		    'id' => $aData['id'],  
            'location_url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'view/' . $aData['uri'],
            'location_title' => $aData['title'],
            'create_date' => $sCreateDate,
            'due_date' => $sDueDate, 
            'author_username' => $sAuthorName,
            'author_url' => $sAuthorLink,
            'invoice_no' => $aData['invoice_no'],
            'order_no' => $aData['order_no'],
            'package' => $sPackageName, 
            'product_status' => $this->getStatus($aData['status']),
            'order_status' => $this->getStatus($aData['order_status']),
			'payment_method' => $aData['payment_method'],
            'invoice_no' => $aData['invoice_no'],
		

         );
 
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }

    function invoice_unit ($aData, $sTemplateName, &$oVotingView) {

        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxLocationModule');
 
        $sAuthorName = getNickName($aData['author_id']); 
		$sAuthorLink = getProfileLink($aData['author_id']);  
 		$sCreateDate = $this->filterDate($aData['invoice_date']);
  		$sDueDate = $this->filterDate($aData['invoice_due_date']);
  		$sExpiryDate = $this->filterDate($aData['invoice_expiry_date']);
 
		$sPackageName = $this->_oMain->_oDb->getPackageName($aData['package_id']);

        $aVars = array (
 		    'id' => $aData['location_id'],  
            'location_url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'view/' . $aData['uri'],
            'location_title' => $aData['title'],
            'create_date' => $sCreateDate,
            'due_date' => $sDueDate, 
            'expiry_date' => $sExpiryDate, 
            'author_username' => $sAuthorName,
            'author_url' => $sAuthorLink,
            'invoice_id' => $aData['id'], 
            'invoice_no' => $aData['invoice_no'],
            'package' => $sPackageName, 
            'invoice_status' => $this->getStatus($aData['invoice_status']),
            'total' => number_format($aData['price']),

         );
 
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }


    function unit ($aData, $sTemplateName, &$oVotingView) {

        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxLocationModule');

        if (!$this->_oMain->isAllowedView ($aData)) {            
            $aVars = array ('extra_css_class' => 'modzzz_location_unit');
            return $this->parseHtmlByName('browse_unit_private', $aVars);
        }
 
 		$aCategory = $this->oDb->getCategoryInfo($aData['category_id']);
		$sCategoryUrl = $this->oDb->getSubCategoryUrl($aCategory['uri']);
 
        $aAuthor = getProfileInfo($aData['author_id']);
/*
        $sImageUrl = ''; 
        $sImageTitle = ''; 
        $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
        $aImage = BxDolService::call('photos', 'get_image', array($a, 'file'), 'Search');
*/

        $sImage = '';
        if ($aData['thumb']) {
            $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
            $aImage = BxDolService::call('photos', 'get_image', array($a, 'browse'), 'Search');
            $sImage = $aImage['no_image'] ? '' : $aImage['file'];
        } 
 
        modzzz_location_import('Voting');
        $oRating = new BxLocationVoting ('modzzz_location', $aData['id']);
 
		$sOwnerThumb = $GLOBALS['oFunctions']->getMemberIcon($aAuthor['ID'], 'left'); 
	  
		$sDateTime = defineTimeInterval($aData['created']);
     
		$sPostText = $aData['desc'];
		$iLimitChars = (int)getParam('modzzz_location_max_preview');
		if (strlen($sPostText) > $iLimitChars) {
 			$sLinkMore = '...';
			$sPostText = mb_substr(strip_tags($sPostText), 0, $iLimitChars) . $sLinkMore;
		}
 
        $aVars = array (
 		    'id' => $aData['id'],  
			'image_url' => !$aImage['no_image'] && $aImage['file'] ? $aImage['file'] : $this->getIconUrl('no-photo-110.png'),
            'image_title' => !$aImage['no_image'] && $aImage['title'] ? $aImage['title'] : '',        
			
            'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'), 
    
            'location_url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'view/' . $aData['uri'],
            'location_title' => $aData['title'],
            'location_description' => $sPostText,
            'comments_count' => $aData['comments_count'],
            'post_date' => strtolower($sDateTime), 
            'post_uthumb' => $sOwnerThumb,
            'post_picture2' => $sPostPicture, 
            'all_categories' => '<a href="'.$sCategoryUrl.'">'.$aCategory['name'].'</a>',  
            'post_tags' => $this->_oMain->parseTags($aData['tags']), 
            'author_title' => _t('_From'),
            'author_username' => $sAuthorName,
            'author_url' => $sAuthorLink,
            'rating' => $oRating->isEnabled() ? $oRating->getJustVotingElement (true, $aData['id']) : '',
        );
 
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }

    // ======================= ppage compose block functions 
 
    function blockLogo ($iPhotoId) {
 
        if ($iPhotoId) { 
			$aFile = BxDolService::call('photos', 'get_photo_array', array($iPhotoId, 'browse'), 'Search');
			$sLogo = $aFile['no_image'] ? '' : $aFile['file'];
		}

		if(!$sLogo)
			return;

        $aVars = array (
             'url' => $sLogo,  
        );
        return $this->parseHtmlByName('block_logo', $aVars);
    }

    function blockDesc (&$aDataEntry) {
        $aVars = array (
             'description' => $aDataEntry['desc'],  
        );
        return $this->parseHtmlByName('block_description', $aVars);
    }

    function blockFields (&$aDataEntry) {
        
		$bHasValues = false;

		$sRet = '<table class="modzzz_location_fields">';
        modzzz_location_import ('FormAdd');        
        $oForm = new BxLocationFormAdd ($GLOBALS['oBxLocationModule'], $_COOKIE['memberID']);
        foreach ($oForm->aInputs as $k => $a) {
            if (!isset($a['display'])) continue;
 
            $sRet .= '<tr><td class="modzzz_location_field_name" valign="top">'. $a['caption'] . '<td><td class="modzzz_location_field_value">';
            if (is_string($a['display']) && is_callable(array($this, $a['display'])))
                $sRet .= call_user_func_array(array($this, $a['display']), array($aDataEntry[$k]));
            else
                $sRet .= $aDataEntry[$k];
            $sRet .= '<td></tr>';

			$bHasValues = true; 
        }

		if(!$bHasValues)
			return;

        $sRet .= '</table>';

        return $sRet;
    }


    function blockSubProfileFields ($sType, &$aDataEntry) {
        
		$bHasValues = false;

		$sRet = '<table class="modzzz_location_fields">';
	 
		switch($sType){
			case 'event';
				modzzz_location_import ('EventFormAdd');   
				$oForm = new BxLocationEventFormAdd ($GLOBALS['oBxLocationModule'], $_COOKIE['memberID']);
			break;
			case 'shop';
				modzzz_location_import ('ShopFormAdd');  
				$oForm = new BxLocationShopFormAdd ($GLOBALS['oBxLocationModule'], $_COOKIE['memberID']);
			break;
 			case 'school';
				modzzz_location_import ('SchoolFormAdd');  
				$oForm = new BxLocationSchoolFormAdd ($GLOBALS['oBxLocationModule'], $_COOKIE['memberID']);
			break;
			case 'service';
				modzzz_location_import ('ServiceFormAdd');  
				$oForm = new BxLocationServiceFormAdd ($GLOBALS['oBxLocationModule'], $_COOKIE['memberID']);
			break; 
			case 'nightlife';
				modzzz_location_import ('NightlifeFormAdd');  
				$oForm = new BxLocationNightlifeFormAdd ($GLOBALS['oBxLocationModule'], $_COOKIE['memberID']);
			break;
			case 'accomodation';
				modzzz_location_import ('HotelFormAdd');  
				$oForm = new BxLocationHotelFormAdd ($GLOBALS['oBxLocationModule'], $_COOKIE['memberID']);
			break;
			case 'food';
				modzzz_location_import ('FoodFormAdd');    
				$oForm = new BxLocationFoodFormAdd ($GLOBALS['oBxLocationModule'], $_COOKIE['memberID']);
			break;
			case 'transport';
				modzzz_location_import ('TransportFormAdd');  
				$oForm = new BxLocationTransportFormAdd ($GLOBALS['oBxLocationModule'], $_COOKIE['memberID']);
			break; 
		}
        
		foreach ($oForm->aInputs as $k => $a) {
            if (!isset($a['display'])) continue;
            if (!$aDataEntry[$k]) continue;
            if ($a['listname']  && $aDataEntry[$k]=='S') continue;

            $sRet .= '<tr><td class="modzzz_location_field_name" valign="top">'. $a['caption'] . '<td><td class="modzzz_location_field_value">';
            if (is_string($a['display']) && is_callable(array($this, $a['display']))){
                

				if($a['name'] == 'state'){
					$sRet .= $this->getStateName($aDataEntry['country'], $aDataEntry[$k]);
				}else{
					if($a['listname'])
						$sRet .= call_user_func_array(array($this, $a['display']), array($a['listname'],$aDataEntry[$k]));
					else
						$sRet .= call_user_func_array(array($this, $a['display']), array($aDataEntry[$k]));
				}
            }else{
                $sRet .= $aDataEntry[$k];
			}
            $sRet .= '<td></tr>';

			$bHasValues = true; 
        }

		if(!$bHasValues)
			return;

        $sRet .= '</table>';

        return $sRet;
    }
  
	function getPreListDisplay($sName, $sVal){ 
 		return htmlspecialchars_adv( _t($GLOBALS['aPreValues'][$sName][$sVal]['LKey']) );
	}
  
    function parseMultiPreValues ($sName, $sVal='') {  
		$sStr = '';
		$aVals = split(';',$sVal);
		foreach($aVals as $aEachVal){
			if($GLOBALS['aPreValues'][$sName][$aEachVal]['LKey'])
 				$sStr .= htmlspecialchars_adv( _t($GLOBALS['aPreValues'][$sName][$aEachVal]['LKey'])) . '<br>';
			else
 				$sStr .= $aEachVal . '<br>'; 
		}
 		return $sStr;
 	}

	function getWebsiteLink($sField='', $sVal=''){ 

		$sLink = (substr($sVal,0,3)=="www") ? "http://" . $sVal : $sVal; 
 
		return '<a href="'.$sLink.'" target=_blank>'.$sLink.'</a>';
	}

	function getStateName($sCountry, $sState){ 
 
		return $this->oDb->getStateName($sCountry, $sState);
	}
   
    function blockCustomFields (&$aDataEntry, $aShow=array()) {
        
		$bHasValues = false;
	
		$sRet = '<table class="modzzz_location_fields">';
        modzzz_location_import ('FormAdd');        
        $oForm = new BxLocationFormAdd ($GLOBALS['oBxLocationModule'], $_COOKIE['memberID']);
        foreach ($oForm->aInputs as $k => $a) {
            if (!isset($a['display'])) continue;
 
            if (!in_array($a['name'],$aShow)) continue;
            
			if (!trim($aDataEntry[$k])) continue;

            $sRet .= '<tr><td class="modzzz_location_field_name" valign="top">'. $a['caption'] . '<td><td class="modzzz_location_field_value">';
            if (is_string($a['display']) && is_callable(array($this, $a['display']))){

				if($a['name'] == 'state'){
					$sRet .= $this->getStateName($aDataEntry['country'], $aDataEntry[$k]);
				}else{
					$sRet .= call_user_func_array(array($this, $a['display']), array($a['listname'],$aDataEntry[$k]));
				}
			}else{
				if($a['name'] == 'businesswebsite')
					$sRet .= "<a target=_blank href='".((substr($aDataEntry[$k],0,3)=="www") ? "http://".$aDataEntry[$k] : $aDataEntry[$k])."'>".$aDataEntry[$k]."</a>";
				else
					$sRet .= $aDataEntry[$k];
			}
            $sRet .= '<td></tr>';

			$bHasValues = true; 
        }

		if(!$bHasValues)
			return;

        $sRet .= '</table>';
        return $sRet;
    }
 
	function getOptionDisplay($sField='',$sVal='')
	{ 
		return ucwords($sVal);
	}
  
	function getStatus($sStatus){
		switch($sStatus){
			case "pending":
				$sLangStatus = _t("_modzzz_location_pending");
			break;
			case "paid":
				$sLangStatus = _t("_modzzz_location_paid");
			break;
			case "active":
				$sLangStatus = _t("_modzzz_location_active");
			break;
			case "inactive":
				$sLangStatus = _t("_modzzz_location_inactive");
			break;
			case "approved":
				$sLangStatus = _t("_modzzz_location_approved");
			break;
			case "expired":
				$sLangStatus = _t("_modzzz_location_expired");
			break;

		}

		return $sLangStatus;
	}

    function filterDate ($i, $bLongFormat = false) {
		if($bLongFormat)
			return getLocaleDate($i, BX_DOL_LOCALE_DATE) . ' ('.defineTimeInterval($i) . ')'; 
		else
			return getLocaleDate($i, BX_DOL_LOCALE_DATE);
	}


	function displayAvailableLevels($aValues) {
	    $sCurrencyCode = strtolower($this->_oConfig->getCurrencyCode());
	    $sCurrencySign = $this->_oConfig->getCurrencySign();
 
	    $aMemberships = array();
	    foreach($aValues as $aValue) { 
  
            $aMemberships[] = array(
                'url_root' => BX_DOL_URL_ROOT,
                'id' => $aValue['id'],
                'title' => $aValue['name'],
                'description' => str_replace("\$", "&#36;", $aValue['description']),
                'days' => $aValue['days'] > 0 ?  $aValue['days'] . ' ' . _t('_membership_txt_days') : _t('_membership_txt_expires_never') ,
                'price' => $aValue['price'],
                'currency_icon' => $this->getIconUrl($sCurrencyCode . '.png'),
 	        );
	    }

		$aVars = array('bx_repeat:levels' => $aMemberships);

	    $this->addCss('levels.css');
	    return $this->parseHtmlByName('available_packages', $aVars);
	}

    function parseSubCategories ($sType, $sVal='') {
		
		$sName = 'Location'.ucwords($sType);
		$sStr = '';
		$aVals = explode(';',$sVal);
		foreach($aVals as $sEachVal){
			$strEachVal = $GLOBALS['aPreValues'][$sName][$sEachVal]['LKey'];
			if($strEachVal)
				$sStr .= htmlspecialchars_adv( _t($strEachVal)) . '&#160' .'<br>';
			else
				$sStr .= htmlspecialchars_adv( _t($sEachVal)) . '&#160' .'<br>';
 		}
  
 		return $sStr;
 	}
 
    function parseSubTags($s){ 
        $sRet = '';
        $a = explode (',', $s);
         
        foreach ($a as $sName)
            $sRet .= $sName.'&#160';
  
        return $sRet;
    }
 

}

?>
